//
//  X-DropListView.swift
//  DouYu
//
//  Created by 王鹏宇 on 10/26/18.
//  Copyright © 2018 王鹏宇. All rights reserved.
//

import UIKit

public enum  ListType {
    
    case LoopBtnType
    case AutomaticType
    case CommenType
}

class X_DropListView: UIView {

    var  loopSwith : UISwitch = UISwitch()
    var  automaticSwith : UISwitch = UISwitch()
    var  Rect : CGRect = CGRect.zero
//    var  overView : UIView?
    
    var automaticChanged:(()->Void)?
    
    class func SetupDropListView(frame : CGRect,typeList : [ListType],contentView : UIView)->X_DropListView {
        
        var rect = frame
        rect.size.height =  (frame.size.height + 20)
        let dropListView = X_DropListView(frame: rect)
            dropListView.backgroundColor = UIColor.white
        dropListView.tag = 999
        
        dropListView.backgroundColor = UIColor.clear
        UIApplication.shared.keyWindow?.addSubview(dropListView)
        dropListView.isHidden = true
        dropListView.Rect = frame
        
        let arrowImageView = UIImageView()
        if let image = UIImage(named: "向上箭头"){
            arrowImageView.image = image
        }
        arrowImageView.contentMode = .scaleAspectFit
        dropListView.addSubview(arrowImageView)

        arrowImageView.snp.makeConstraints { (make) in
            make.trailing.equalTo(dropListView.snp.trailing).offset(-7)
            make.top.equalTo(dropListView.snp.top).offset(8)
            make.width.equalTo(30)
            make.height.equalTo(15)
        }
        
        
        dropListView.setupWithArray(arr: typeList)
        
        dropListView.layer.cornerRadius = 5
        dropListView.clipsToBounds = true
        
        return dropListView
    }
    
    
    func show(){
        let rect = CGRect(x:(self.Rect.origin.x), y: self.Rect.origin.y , width:self.Rect.width, height: 1)
        
        self.frame = rect
        UIApplication.shared.keyWindow?.addSubview(self)
        self.isHidden = false
        
        if let overView = UIApplication.shared.keyWindow?.viewWithTag(110){
            
            overView.isHidden = false
        }else{
            
            let  overView = UIView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: kScreenHeight))
            overView.backgroundColor = UIColor.clear
            UIApplication.shared.keyWindow?.insertSubview(overView, belowSubview: self)
            let tap = UITapGestureRecognizer(target: self, action: #selector(tapAction))
            overView.addGestureRecognizer(tap)
            overView.tag = 110
        }
        
        UIView.animate(withDuration: 0.3, animations: {
            self.frame = self.Rect
            
        }) { (finished) in
            
        }
    }
    
    @objc func tapAction(){
    
         self.hidde()
    }
    
    func hidde(){
        
        let rect = CGRect(x: (self.Rect.origin.x), y: self.Rect.origin.y, width:self.Rect.width, height: 1)
        
        if   let overView = UIApplication.shared.keyWindow?.viewWithTag(110){
            overView.isHidden = false
            overView.removeFromSuperview()
        }
        
        UIView.animate(withDuration: 0.3, animations: {
            self.frame = rect
            
        }) { (finished) in
           self.isHidden = true
           self.removeFromSuperview()
        }
    }
    
    func setupWithArray(arr : [ListType]){
        
        let contentView = UIView(frame: CGRect(x: 0, y: 20, width: self.frame.width, height: self.frame.height - 20))
        contentView.layer.cornerRadius = 5
        contentView.clipsToBounds = true
        contentView.backgroundColor = UIColor.white
        self.addSubview(contentView)
        
        let H = 40
        let W = 105
        
        for i in 0..<arr.count{
            
            var view : UIView!
            
            switch arr[i] {
                
            case .LoopBtnType:
                
                view = getLoopSwithView()
                break
            case .AutomaticType:
                view = getAutomaticSwithView()
                break
            case .CommenType:
                break
            }
            
            view.frame = CGRect(x: 0, y:(H * i), width: W, height: H)
            view.backgroundColor = UIColor.white
            if i > 0{
                let lineView = UIView(frame: CGRect(x: 2, y: 0, width: W - 4, height: 1))
                lineView.backgroundColor = UIColor.groupTableViewBackground
                view.addSubview(lineView)
            }
        contentView.addSubview(view)
        }
        
    }
    
    
    func getLoopSwithView()->UIView {
        
        let contentView = UIView()
        let label = UILabel()
        
        loopSwith = UISwitch()
        contentView.addSubview(loopSwith)
        loopSwith.transform = CGAffineTransform.init(scaleX: 0.6, y: 0.6)
        
        loopSwith.isOn = AppManager.share.allPlayWhenListen
        loopSwith.addTarget(self, action:#selector(loopAction), for: UIControl.Event.valueChanged)
        loopSwith.snp.makeConstraints { (make) in
            make.trailing.equalTo(contentView.snp.trailing).offset(2)
            make.centerY.equalTo(contentView.snp.centerY)
        }
        
        
        contentView.addSubview(label)
        label.textColor = UIColor.black
        label.font = UIFont.systemFont(ofSize: 11)
        label.textAlignment = .center
        label.text = LocalizedString("continued_play")
        label.snp.makeConstraints { (make) in
            make.leading.equalTo(contentView.snp.leading).offset(8)
            make.top.bottom.equalToSuperview()
            make.trailing.equalTo(loopSwith.snp.leading).offset(0)
        }
        
        
        return contentView
    }
    
    
    func getAutomaticSwithView()->UIView {
        
        let contentView = UIView()
        let label = UILabel()
        
        automaticSwith = UISwitch()
        contentView.addSubview(automaticSwith)
        automaticSwith.transform = CGAffineTransform.init(scaleX: 0.6, y: 0.6)
        automaticSwith.isOn = AppManager.share.automaticPlay
         automaticSwith.addTarget(self, action:#selector(automaticAction), for: UIControl.Event.valueChanged)
        automaticSwith.snp.makeConstraints { (make) in
            make.trailing.equalTo(contentView.snp.trailing).offset(2)
            make.centerY.equalTo(contentView.snp.centerY)
        }
        
        
        contentView.addSubview(label)
        label.textColor = UIColor.black
        label.font = UIFont.systemFont(ofSize: 11)
        label.textAlignment = .center
        label.text = LocalizedString("auto_play")
        label.snp.makeConstraints { (make) in
            make.leading.equalTo(contentView.snp.leading).offset(8)
            make.top.bottom.equalToSuperview()
            make.trailing.equalTo(automaticSwith.snp.leading).offset(0)
        }
        
        return contentView
    }
    
    
    @objc func loopAction(){
        
        if AppManager.share.allPlayWhenListen {
            
            AppManager.share.allPlayWhenListen = false
        }else{
            AppManager.share.allPlayWhenListen = true
            self.automaticSwith.setOn(false, animated: true)
            AppManager.share.automaticPlay = false
        }
    }
    
    @objc func automaticAction(){
        
        if AppManager.share.automaticPlay {
            AppManager.share.automaticPlay = false
        }else{
            AppManager.share.automaticPlay = true
            self.loopSwith.setOn(false, animated: true)
            AppManager.share.allPlayWhenListen = false
        }
        
        automaticChanged?()
    }
}
